package com.yuexun.beilunpatient.ui.family.adapter;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.ui.family.bean.KinsfolkBean;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.Collection;

/**
 * Created by chenss on 2018/10/26.
 */
public class FamilyListAdapter extends KJAdapter<KinsfolkBean> {

    boolean isSelect;

    public void setSelect(boolean select) {
        isSelect = select;
        notifyDataSetChanged();
    }

    ICallBackListener listener;

    public void setListener(ICallBackListener listener) {
        this.listener = listener;
    }

    public FamilyListAdapter(AbsListView view, Collection<KinsfolkBean> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
    }

    @Override
    public void convert(AdapterHolder helper, final KinsfolkBean item, boolean isScrolling, final int position) {
        super.convert(helper, item, isScrolling, position);

        helper.setText(R.id.tv_name, item.getName());
        if(item.getRel_type().equals("1")){
            helper.setText(R.id.tv_relation, "父母");
        }else if(item.getRel_type().equals("2")){
            helper.setText(R.id.tv_relation, "子女");
        }else if(item.getRel_type().equals("3")){
            helper.setText(R.id.tv_relation, "其他");
        }
        helper.setText(R.id.tv_ids, "身份证号：" + item.getIdcard());
        helper.setText(R.id.tv_phone, "手机号：" + item.getPhone());
        TextView textView = helper.getView(R.id.tv_relieve);

        ImageView selectView = (ImageView) helper.getView(R.id.select_view);

        if(isSelect){
            selectView.setVisibility(View.VISIBLE);
        }else{
            selectView.setVisibility(View.GONE);
        }

        if(item.isSelect()){
            selectView.setImageResource(R.drawable.select_checked);
        }else{
            selectView.setImageResource(R.drawable.select_unchecked);
        }

        selectView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(item.isSelect()){
                    item.setSelect(false);
                }else{
                    item.setSelect(true);
                }
                notifyDataSetChanged();
            }
        });

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                listener.callback(bundle);
            }
        });
    }
}
